package org.teamh.gui.ingame.effects;

import static org.lwjgl.opengl.GL11.GL_LIGHTING;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glDisable;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex3d;

import java.nio.FloatBuffer;

import org.teamh.logic.datamodel.Creep;

public class CreepLife extends Effect{

	private Creep creep;
	
	private float width = 1 / 16f;
	private float height = width / 5;
	private float posY = 1.2f / 16f;
	
	private FloatBuffer modelviewMatrix;
	
	public CreepLife(Creep creep) {
		this.creep = creep;
		
		modelviewMatrix = getEffectManager().getIngameFunctions().getModelviewMatrix();
	}
	
	@Override
	public void render(float timeElapsed) {
		glDisable(GL_LIGHTING);
		glDisable(GL_TEXTURE_2D);
		if(creep.getHealth() <= 0 || creep.getUserAssets() == null) {
			getEffectManager().removeEffect(this);
		}
		
		float health = 1;
		
		if(creep.getCreepDescription() != null) {
			health = creep.getHealth() / (float)creep.getCreepDescription().getHealthMax();
		}
		
		glBegin(GL_QUADS);
		{
			glColor3f(1, 0, 0);
			glVertex3d(creep.getWorldX() - width / 2, posY, creep.getWorldZ());
			glVertex3d(creep.getWorldX() - width / 2, posY + modelviewMatrix.get(5) * height, creep.getWorldZ() + modelviewMatrix.get(9) * height);
			glVertex3d(creep.getWorldX() - width / 2 + width, posY + modelviewMatrix.get(5) * height, creep.getWorldZ() + modelviewMatrix.get(9) * height);
			glVertex3d(creep.getWorldX() - width / 2 + width, posY, creep.getWorldZ());
			
			glColor3f(0, 1, 0);
			glVertex3d(creep.getWorldX() - width / 2, posY, creep.getWorldZ());
			glVertex3d(creep.getWorldX() - width / 2, posY + modelviewMatrix.get(5) * height, creep.getWorldZ() + modelviewMatrix.get(9) * height);
			glVertex3d(creep.getWorldX() - width / 2 + width * health, posY + modelviewMatrix.get(5) * height, creep.getWorldZ() + modelviewMatrix.get(9) * height);
			glVertex3d(creep.getWorldX() - width / 2 + width * health, posY, creep.getWorldZ());
		}
		glEnd();
		glColor3f(1, 1, 1);
	}
}
